<?php
/*
 * @Author       : chaihao
 * @Date         : 2020-06-01 10:58:11
 * @FilePath     : /ai/controllers/api/SeatController.php
 */

namespace app\controllers\api;

use yii;
use app\commands\ErrorMsg;
use app\commands\Helper;
use app\commands\Validator;
use app\Services\api\SeatService;


class SeatController extends ApiController
{

    /**
     * 房间占座情况
     */
    public function actionSeatInfo()
    {
        $req = $this->req;
        # 请求参数验证
        $rules = array(
            'roomid'  => "required"
        );
        Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
        $res = SeatService::getInstance()->getSeatInfo($req['roomid']);
        Helper::right_out($req, $res);
    }
    /**
     * 返回SeattimeInfo表中比传参时间大的所有status=1和2的记录
     */
    public function actionSeattimeInfo()
    {
        $req = $this->req;;
        $rules = array(
            'roomid'  => "required",
            'updatetime'  => "required"
        );
        Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
        $roomid = $req['roomid'];
        $updatetime = $req['updatetime'];
        $res = SeatService::getInstance()->seattimeInfo($roomid, $updatetime);
        Helper::right_out($req, $res);
    }
    /**
     * 从已坐好状态下离开
     */
    public function actionQuitSeat()
    {
        $req = $this->req;
        $rules = array(
            'roomid'  => 'required|integer',
            'row' => 'required|integer',
            'col' => 'required|integer',
            'status' => 'required',
            'updatetime' => 'required',
        );
        Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
        $req['user_id'] = $req['userid'] ?? $this->user->ID;
        $res = SeatService::getInstance()->quitSeat($req);
        Helper::right_out($req, $res);
    }
    /**
     * 返回seattimeline表中时间大于updatetime的所有status=1和2的记录
     *
     * @return void
     */
    public function actionUpdateMod()
    {
        $req = $this->req;;
        $rules = array(
            'roomid'  => "required",
            'index'  => 'required|integer',
        );
        Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
        $roomid = $req['roomid'];
        $index = $req['index'];
        $res = SeatService::getInstance()->updateMod($roomid, $index);
        Helper::right_out($req, $res);
    }

    /**
     * 返回seattimeline表与传参roomid和userid相符的最后一条记录
     */
    public function actionCheckHeart()
    {
        $req = $this->req;;
        $rules = array(
            'roomid'  => "required",
        );
        Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
        $roomid = $req['roomid'];
        $user_id = $req['userid'] ?? $this->user->ID;
        $res = SeatService::getInstance()->checkHeart($roomid, $user_id);
        Helper::right_out($req, $res);
    }
    /**
     * 使用seattimelimit表更新seat表，每次调用更新一次。seattimeline表中satus=1的则在seat表对应位置将0改为userid， satus=2则将userid改为0
     */
    public function actionUpdateSeat()
    {
        $req = $this->req;;
        $updatetime = $req['updatetime'] ?? NULL;
        $res = SeatService::getInstance()->updateSeatByIndex();
        Helper::right_out($req, $res);
    }


    /**
     * 返回seattimeline表中updatetime大于传参的status=2 or 4 or 5 or 6的记录
     */
    public function actionGetLiveHeart()
    {
        $req = $this->req;;
        $rules = array(
            'index'  => "required|integer",
            'status'  => "required",
        );
        Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
        $res = SeatService::getInstance()->getLiveHeart($req);
        Helper::right_out($req, $res);
    }

    /**
     * 在seattimeline表中插入传参数值的一条记录
     */
    public function actionUpdateLiveHeart()
    {
        $req = $this->req;;
        $rules = array(
            'roomid'  => 'required|integer',
            'row' => 'required|integer',
            'col' => 'required|integer',
            'status' => 'required|integer',
            'updatetime' => 'required',
        );
        Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
        $req['user_id'] = $req['userid'] ?? $this->user->ID;
        $res = SeatService::getInstance()->updateLiveHeart($req);
        Helper::right_out($req, $res);
    }

    /**
     * 确认选座，在seattimeline表中插入status值为1，其他数据满足传参的一条记录
     */
    public function actionConfirmSeat()
    {
        $req = $this->req;
        $rules = array(
            'roomid'  => 'required|integer',
            'row' => 'required|integer',
            'col' => 'required|integer',
            // 'updatetime' => 'required',
        );
        Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
        $req['user_id'] = $req['userid'] ?? $this->user->ID;
        $res = SeatService::getInstance()->confirmSeat($req);
        Helper::right_out($req, $res);
    }
    /**
     * 更新在线状态
     *
     */
    public function actionOnlineState()
    {
        $req = $this->req;
        $res = SeatService::getInstance()->onlineState();
        Helper::right_out($req, $res);
    }
}
